<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mreview extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
   
    public function getReviewDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $sql  = '';
        $sql = "SELECT r.id, r.name as name_res, m.id, m.fullname, rr.id as rid, rr.name as name_re, rr.star, rr.content, rr.improved, rr.created 
		FROM restaurant_review as rr 
		LEFT JOIN restaurant as r ON r.id = rr.restaurant_id 
		LEFT JOIN member as m ON m.id = rr.user_id 
		
		WHERE 1=1 ";
		
		
		$keyword = $this->getParamString('keyword');
        if(!in_array($keyword, array(null, ''))){
            $sql .= "AND r.`name` LIKE '%".$keyword."%' ";
        }
		
		
        $sql .= "ORDER BY rr.id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();

        foreach($xs AS $el=>$le)
       {
			if( in_array($le->fullname,array('',null,0))){
				$xs[$el]->fullname = $le->name_re;
			}
       }
        
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);   
    }

	public function deleteReviewDb($id){
        $this->db->delete('restaurant_review', array('id' => $id)); 
        return 200;
	}
	
	public function getReviewInfo($id){
		return $this->db->query("SELECT r.id as rid, r.name as name_res, m.id as mid, m.fullname, rr.id as rrid, rr.name as name_re, rr.star, rr.content, rr.improved, rr.created FROM restaurant_review as rr LEFT JOIN restaurant as r ON r.id = rr.restaurant_id LEFT JOIN member as m ON m.id = rr.user_id WHERE rr.id = '".$id."'")->row_object();
	}
	
	public function editReview($id,$restaurant_id,$star,$content,$improved){
		$data = array(
			'restaurant_id' => $restaurant_id,
            'star' => $star,
			'content' => $content,
			'improved' => $improved
        );
        $this->db->update('restaurant_review', $data, "id = ".$id);
        return 200;
	}
	
	public function optionRestaurant($id)
    {
        $data = $this->db->query('SELECT id, name FROM restaurant ORDER BY id ASC')->result_object();
        $html = '<option value = "">Chọn nhà hàng</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->name.'</option>';
        }
        return $html;
    }
	
	public function optionMember($id)
    {
        $data = $this->db->query('SELECT id, username,fullname FROM member ')->result_object();
        $html = '<option value = "">Chọn người dùng</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->fullname.'</option>';
        }
        return $html;
    }
	
	public function optionStar($id)
    {
        $html = '<option value = "">Chọn số sao</option>';
        for ( $i = 1; $i <=5; $i++  ) 
        {
            if($i == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$i.'" '.$selected.'>'.$i.' Sao</option>';
        }
        return $html;
    }   
		
	public function nameCustomer($name,$mid){
		if ( in_array($mid,array('',null,0))){
			
			$data = $name;
			if ( in_array($data,array('',null)) ){
				$data = 'Đang cập nhật';
			}
		}else{
			$db = $this->db->query('SELECT fullname,username FROM member WHERE id = '.$mid.' LIMIT 1');
			if( $db->num_rows() == 0){
				
				$data = 'Đang cập nhật';
			}else{
				$data = $db->row()->fullname;
				if ( in_array($data,array('',null)) ){
					$data = $db->row_array['username'];
					if ( in_array($data,array('',null)) ){
						$data = 'Đang cập nhật';
					}
				}
			}
		}
		return $data;
	}	
	
}
 